﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WiseLinkWPF.Windows.Controls.Map;
using System.Collections.ObjectModel;
using CCF20Model;
using WiseLinkWPF.Windows.Controls;
using CCF20.ZICModel;

namespace CCF20
{
    public partial class HelpMap : Window
    {
        private MapItemsCollection itemCollection = new MapItemsCollection();
        private BingSearchProvider searchProvider;
        private siteModel _siteModel;
        
        public HelpMap(siteModel location)
        {
            InitializeComponent();
            //ccfZIC = new CCFZIC();
            this.SetProvider();
            _siteModel = location;
            //this.informationLayer.DataMappings.Add(
            //    new DataMapping("Location", DataMember.Location));

            //Binding binding = new Binding();
            //binding.Source = this.itemCollection;
            //this.informationLayer.SetBinding(ItemsControl.ItemsSourceProperty, binding);
            //vm = new VehicleModel();
		
        }
        private void SetProvider()
        {
            //BingMapProvider provider = new BingMapProvider(MapMode.Aerial, true, BingMapHelper.VEKey);
            //provider.IsTileCachingEnabled = true;

            OpenStreetCustomProvider provider = new OpenStreetCustomProvider(MapMode.Road, true);
            provider.IsTileCachingEnabled = true;

            
            this.RadMap1.Provider = provider;
            
            // Init searh provider.
            searchProvider = new BingSearchProvider();
            searchProvider.ApplicationId = BingMapHelper.VEKey;

            searchProvider.MapControl = this.RadMap1;
            
            searchProvider.SearchCompleted += new EventHandler<SearchCompletedEventArgs>(Provider_SearchCompleted);
        }
        private void SearchHandler(object sender, RoutedEventArgs e)
		{
            string query = null;//this.SearchCondition.Text;

			if (!string.IsNullOrEmpty(query))
			{
				SearchRequest request = new SearchRequest();
                request.Culture = new System.Globalization.CultureInfo("zh-CN");
                request.Query = query;

				this.searchProvider.SearchAsync(request);
			}
		}
        private void Provider_SearchCompleted(object sender, SearchCompletedEventArgs args)
        {
            SearchResponse response = args.Response;
            
            if (response != null && response.ResultSets.Count > 0)
            {
                this.itemCollection.Clear();
                if (response.ResultSets[0].Results.Count > 0)
                {
                    
                    foreach (SearchResultBase result in response.ResultSets[0].Results)
                    {
                        MyMapItem item = new MyMapItem()
                        {
                            Title = result.Name,
                            Location = result.LocationData.Locations[0]
                        };
                       

                        this.itemCollection.Add(item);
                    }
                }

                if (response.ResultSets[0].SearchRegion != null)
                {
     
                    if (response.ResultSets[0].SearchRegion.GeocodeLocation.Address != null
                        && response.ResultSets[0].SearchRegion.GeocodeLocation.Locations.Count > 0)
                    {
                        foreach (Location location in response.ResultSets[0].SearchRegion.GeocodeLocation.Locations)
                        {
                            MyMapItem item = new MyMapItem()
                            {
                                Title = response.ResultSets[0].SearchRegion.GeocodeLocation.Address.FormattedAddress,
                                Location = location
                            };
                            this.itemCollection.Add(item);
                            RadMap1.Center = item.Location;
                            this.informationLayer.Items.Clear();
                            this.informationLayer.Items.Add(item.Location);
                        }
                    }
                }
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Search(_siteModel.Latitude, _siteModel.Longitude);
        }
        private void Search(string lat, string lon)
        {
            SearchRequest request = new SearchRequest();
            request.Culture = new System.Globalization.CultureInfo("zh-CN");
            request.Query = string.Format("{0},{1}", lat, lon);
            LoggerHelper.LogCCF20(string.Format("地图查询经度={1}，维度={0}",lat,lon));
            this.searchProvider.SearchAsync(request);
        }

        private void UrgentVehicleList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //UrgentData ud = (UrgentData)e.AddedItems[0];
         
           
            
        }

        private void RadContextMenu_ItemClick(object sender, WiseLinkWPF.Windows.RadRoutedEventArgs e)
        {
            //    RadContextMenu menu = (RadContextMenu)sender;
            //    RadMenuItem clickedItem = e.OriginalSource as RadMenuItem;
            //    GridViewRow row = menu.GetClickedElement<GridViewRow>();

            //    Fault currentFault =  row.Item as Fault;
            //    if (clickedItem != null && row != null)
            //    {
            //        string header = Convert.ToString(clickedItem.Header);



            //    string header = (e.OriginalSource as RadMenuItem).Header as string;

            //    switch (header)
            //    {
            //        case "显示地图":

            //            break;
            //        case "排除":

            //            break;
            //        case "联系":
            //            break;
            //}
        }
    }
}
